import e from "jquery";

class UrlUtil {
  static origin() {
    let e = String(window.location.href),
      t = /^http[s]?:\/\/[^\/]*\/{1}/,
      n = null;
    return t.test(e) ? (n = e.match(t)[0]) : (n = e + "/"), n;
  }
  static relativePath(e) {
    let n = this.cleanURL(),
      r = n.split("/");
    r.pop();
    let i = e.split("/");
    for (let s = 0; s < i.length; s++) {
      let o = i[s];
      o == ".." ? r.pop() : o.length > 0 && o != "." && r.push(o);
    }
    let u = r.join("/");
    return u;
  }
  static cleanURL() {
    let e = String(window.location.href),
      t = e.split("#");
    return t[0];
  }
  static hash(e) {
    if (arguments.length > 0) {
      let n = this.cleanURL();
      window.location.href = n + "#" + encodeURIComponent(e);
    }
    let r = String(window.location.href),
      i = r.split("#"),
      s = null;
    return i.length > 1 && (s = decodeURIComponent(i[1])), s;
  }
  static decodeParams(t) {
    let n = null;
    if (t != null) {
      n = {};
      let r = decodeURIComponent(t).split("&");
      e.each(r, function (e, t) {
        let r = t.split("=")[0],
          i = decodeURIComponent(t.split("=")[1]);
        n[r] = i;
      });
    }
    return n;
  }
  static encodeParams(e) {
    let t = "";
    if (e != null) {
      let n = [];
      for (let r in e)
        if (e.hasOwnProperty(r)) {
          let i = encodeURIComponent(e[r]);
          n.push(r + "=" + i);
        }
      t = n.join("&");
    }
    return t;
  }
  static splitURLs(e) {
    let t = /\[\d+-\d+\]/,
      n = e.match(t),
      r = [];
    if (n != null) {
      n = n[0];
      let i = n.slice(1, n.length - 1).split("-"),
        s = i[0],
        o = i[1],
        u = Math.max(s.length, o.length);
      (s = parseInt(s, 10)), (o = parseInt(o, 10));
      for (let a = s; a <= o; a++) {
        let f = String(a);
        while (f.length < u) f = "0" + f;
        let l = e.replace(t, f);
        r.push(l);
      }
    } else r = [e];
    return r;
  }
}

export default UrlUtil;
